// pages/course/index.js
import dayjs from 'dayjs';
import {
    shouldFresh,
    orderListFinishFresh
} from '@/utils/orderListFresh';
import {
    getCourseList
} from '@/services/course/index.js'
import {
    apiUrl
} from '@/config/index'
import {
    useDict
} from '@/utils/dict'
Page({
    page: {
        size: 999,
        num: 1,
    },
    /**
     * 页面的初始数据
     */
    data: {
        apiUrl,
        tabs: [{
                key: 0,
                text: '待预约',
                total: 0
            },
            {
                key: 1,
                text: '待上课',
                total: 0
            },
            {
                key: 2,
                text: '已上课',
                total: 0
            },
            {
                key: 3,
                text: '开课失败',
                total: 0
            }
        ],
        status: 0,
        courseList: [],
        pullDownRefreshing: false,
        loadingProps: {
            theme: 'circular',
            size: '40rpx',
        },
        userInfo: null
    },
    initPage() {
        useDict('course_type').then((res) => {
            this.setData({
                typeList: [{
                        label: '全部',
                        value: ''
                    },
                    ...res['course_type']
                ]
            })
        })
    },

    getCourseData(status, reset = false) {
        getCourseList({
            pageSize: this.page.size,
            pageNumber: this.page.num,
            customerId: this.data.userInfo?.customerId,
            status: status
        }).then((res) => {
            const courseList = reset ? res.data : this.data.courseList.concat(res.data);
            this.setData({
                courseList: courseList
            });
        })
    },

    onTabChange() {
        const {
            value
        } = e.detail;
        this.setData({
            status: value,
        });
        this.refreshList(value);
    },

    refreshList(status = 0) {
        this.setData({
            courseList: []
        });
        return this.getCourseData(status, true);
    },

    onRefresh() {
        return this.refreshList(this.data.curTab);
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        const userInfo = JSON.parse(wx.getStorageSync('userInfo') || '{}')
        if (userInfo && userInfo.customerId) {
            this.setData({
                userInfo: userInfo
            })
            this.getCourseData(this.data.status)
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})